package com.mysterymall.Service.Impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mysterymall.Common.Comp.HttpStatus;
import com.mysterymall.Common.Utils.Result;
import com.mysterymall.Mapper.BankCardMapper;
import com.mysterymall.Mapper.UserMapper;
import com.mysterymall.Model.Entity.BankCard;
import com.mysterymall.Model.Entity.User;
import com.mysterymall.Service.BankCardService;
import org.springframework.stereotype.Service;

/**
 * @ClassName com.mysterymall.Service.Impl
 * @Description
 * @Author HuangWX
 * @Time 2023/12/13  15:48
 */
@Service
public class BankCardServiceImpl implements BankCardService {

    private final BankCardMapper bankCardMapper;


    private final UserMapper userMapper;

    /**
     * 构造器注入
     *
     * @param bankCardMapper 银行卡映射器
     */
    public BankCardServiceImpl(BankCardMapper bankCardMapper, UserMapper userMapper) {
        this.bankCardMapper = bankCardMapper;
        this.userMapper = userMapper;
    }

    @Override
    public Result addBankCard(BankCard bankCard) {

        LambdaQueryWrapper<BankCard> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BankCard::getUserId, StpUtil.getLoginId().toString());
        long count = bankCardMapper.selectCount(lambdaQueryWrapper);

        if (count >= 4) {
            return new Result(400, "message.bankCardCountError");
        }
        bankCard.setUserId(StpUtil.getLoginId().toString());
        bankCardMapper.insert(bankCard);
        return new Result(HttpStatus.SUCCESS, "message.addSuccess");
    }

    @Override
    public Result deleteBankCard(String bankCardId) {

        if (bankCardId == null || bankCardId.isEmpty()) {
            return new Result(HttpStatus.BAD_NOT_PASS, "message.deleteError");
        }

        BankCard bankCard = bankCardMapper.selectById(bankCardId);

        if (!bankCard.getUserId().equals(StpUtil.getLoginId().toString())) {
            return new Result(HttpStatus.BAD_NOT_PASS, "message.powerError");
        }
        bankCardMapper.deleteById(bankCard.getId());
        return new Result(HttpStatus.SUCCESS, "message.deleteSuccess");
    }

    @Override
    public Result updateBankCard(BankCard bankCard) {

        if (bankCard.getId() == null || bankCard.getId().isEmpty()) {
            return new Result(HttpStatus.BAD_NOT_PASS, "message.updateError");
        }
        BankCard bankCard1 = bankCardMapper.selectById(bankCard.getId());
        if (!bankCard1.getUserId().equals(StpUtil.getLoginId().toString())) {
            return new Result(HttpStatus.BAD_NOT_PASS, "message.powerError");
        }
        bankCardMapper.updateById(bankCard);
        return new Result(HttpStatus.SUCCESS, "message.updateSuccess");
    }

    @Override
    public Result getBankCard() {
        LambdaQueryWrapper<BankCard> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BankCard::getUserId, StpUtil.getLoginId().toString());
        return new Result(HttpStatus.SUCCESS, "message.querySuccess", bankCardMapper.selectList(lambdaQueryWrapper));
    }

    @Override
    public Result getBankCardById(String bankCardId) {

        BankCard bankCard1 = bankCardMapper.selectById(bankCardId);

        if (!bankCard1.getUserId().equals(StpUtil.getLoginId().toString())) {
            return new Result(HttpStatus.BAD_NOT_PASS, "message.powerError");
        }

        return new Result(HttpStatus.SUCCESS, "message.querySuccess", bankCard1);
    }

    @Override
    public Result getMyBankCard() {

        // 获取当前登录用户信息
        User user = userMapper.selectById(StpUtil.getLoginIdAsString());

        if (user.getAcquiesceBackCard() == null || user.getAcquiesceBackCard().isEmpty()) {
            return new Result(HttpStatus.BAD_NOT_PASS, "message.powerError");
        }


        // 查询默认银行卡
        BankCard bankCard = bankCardMapper.selectById(user.getAcquiesceBackCard());

        if (bankCard == null) {
            return new Result(HttpStatus.BAD_NOT_PASS, "message.powerError");
        }

        return new Result(HttpStatus.SUCCESS, "message.querySuccess", bankCard);
    }

    @Override
    public Result getBankCardByIdAdmin(String bankCardId) {

        BankCard bankCard = bankCardMapper.selectById(bankCardId);
        if (bankCard == null) {
            return new Result(HttpStatus.BAD_NOT_PASS, "message.queryError");
        }
        return new Result(HttpStatus.SUCCESS, "message.querySuccess", bankCard);
    }

    @Override
    public Result addBankCardAdmin(BankCard bankCard) {

        bankCardMapper.insert(bankCard);

        User user = userMapper.selectById(bankCard.getUserId());

        user.setAcquiesceBackCard(bankCard.getId());
        userMapper.updateById(user);
        return new Result(HttpStatus.SUCCESS, "message.addSuccess");
    }

    @Override
    public Result updateBankCardAdmin(BankCard bankCard) {

        if(bankCard.getId() == null || bankCard.getId().isEmpty()){
            return new Result(HttpStatus.BAD_NOT_PASS, "message.updateError");
        }

        bankCardMapper.updateById(bankCard);
        return new Result(HttpStatus.SUCCESS, "message.updateSuccess");
    }
}
